﻿using LDM.API.Models;
using LDM.Business.ServiceAPI.Svc;
using LDM.Framework.Application;
using LDM.Framework.NormalizeActionResult;
using LDM.Sys.ServiceAPI.Model.DBModel;
using LDM.Sys.ServiceAPI.Model.ViewModel.Request;
using LDM.Sys.ServiceAPI.Model.ViewModel.Response.Login;
using LDM.Sys.ServiceAPI.Svc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Swashbuckle.AspNetCore.Annotations;
using System.Net.Sockets;
using WebApiClientCore.Exceptions;

namespace LDM.API.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    [ApiExplorerSettings(GroupName = ApiModularInfo.User)]
    public class UserController : ControllerBase
    {
        private readonly ILoginService _loginService;
        public UserController(ILoginService loginService)
        {
            _loginService = loginService;
        }

        /// <summary>
        /// 微信用户登录获取token
        /// </summary>
        /// <param name="loginReq"></param>
        /// <returns></returns>
        [AllowAnonymous, HttpPost]
        [SwaggerOperation("微信用户登录"), SwaggerResponse(200, type: typeof(UserLoginRes))]
        public async Task<ResultData> WxLogin(GetWxLoginRequest loginReq)
        {
            var userInfo = await _loginService.GetWxLogin(loginReq);
            return ResultData.Success(userInfo); 
        }

        /// <summary>
        /// 同步微信用户数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [SwaggerOperation("同步微信用户数据"), SwaggerResponse(200, type: typeof(bool))]
        public async Task<ResultData> SyncWxUserInfo(SyncWxUserInfoRequest request) 
        {
            var result = await _loginService.SyncWxUserInfo(request);
            return ResultData.Success(result);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [AllowAnonymous, HttpPost]
        [SwaggerOperation("获取用户信息"), SwaggerResponse(200, type: typeof(WxAccount))]
        public async Task<ResultData> GetUserInfo(string token)
        {
            var userInfo = await _loginService.GetUserInfo(token);
            return ResultData.Success(userInfo);
        }

    }
}
